import 'package:flutter/material.dart';
import 'package:hrcapp/utils/theme_colors.dart';
import 'package:hrcapp/app_routes.dart';

class HRCGenerateTokenScreen extends StatelessWidget {
  const HRCGenerateTokenScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ThemeColors.background,
      appBar: AppBar(
        backgroundColor: ThemeColors.cardBg,
        elevation: 0,
        centerTitle: true,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, size: 16, color: Colors.black),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        title: const Text(
          '生成通证',
          style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
            color: Colors.black,
          ),
        ),
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children:
              [
                // 流程指示器
                _buildProcessIndicator(),
                const SizedBox(height: 40),

                // 资产信息卡片
                _buildAssetInfoCard(),
                const SizedBox(height: 40),

                // 通证生成信息
                _buildTokenInfo(),
                const SizedBox(height: 40),

                // 确认生成按钮 - 移到底部
                _buildConfirmButton(context),
              ],
            ),
          ),
        ),
      ),
    );
  }

  // 构建流程指示器
  Widget _buildProcessIndicator() {
    return SizedBox(
      height: 80,
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        physics: const ClampingScrollPhysics(),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            _buildStepIndicator('资产上传', false, '1'),
            _buildStepConnector(false),
            _buildStepIndicator('认证评估', false, '2'),
            _buildStepConnector(false),
            _buildStepIndicator('价值锚定', false, '3'),
            _buildStepConnector(true),
            _buildStepIndicator('生成通证', true, '4'),
            _buildStepConnector(false, true),
            _buildStepIndicator('上链完成', false, '5'),
          ],
        ),
      ),
    );
  }

  // 构建步骤指示器
  Widget _buildStepIndicator(String label, bool isActive, String number) {
    return Column(
      children: [
        CircleAvatar(
          radius: 15,
          backgroundColor: isActive ? ThemeColors.secondary : const Color(0xFFE5E6EB),
          child: Text(
            number,
            style: const TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.bold,
              color: Colors.white,
            ),
          ),
        ),
        const SizedBox(height: 10),
        Text(
          label,
          style: TextStyle(
            fontSize: 12,
            color: isActive ? ThemeColors.secondary : const Color(0xFF86909C),
          ),
        ),
      ],
    );
  }

  // 构建步骤连接器
  Widget _buildStepConnector(bool isActive, [bool isDashed = false]) {
    return Container(
      height: 2,
      width: 40, // 设置固定宽度以支持水平滚动
      margin: const EdgeInsets.symmetric(horizontal: 4),
      color: isActive ? ThemeColors.secondary : const Color(0xFFE5E6EB),
    );
  }

  // 构建资产信息卡片
  Widget _buildAssetInfoCard() {
    return Container(
      width: double.infinity,
      height: 120,
      decoration: BoxDecoration(
        color: ThemeColors.cardBg,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Padding(
        padding: const EdgeInsets.all(20),
        child:
        Row(
          children: [
            // 资产图片
            Container(
              width: 80,
              height: 80,
              decoration: BoxDecoration(
                color: const Color(0xFFF5F5F5),
                borderRadius: BorderRadius.circular(8),
                border: Border.all(
                  color: ThemeColors.secondary.withOpacity(0.3),
                  width: 1,
                ),
              ),
              child: const Center(
                child: Icon(
                  Icons.art_track,
                  size: 32,
                  color: ThemeColors.secondary,
                ),
              ),
            ),
            const SizedBox(width: 20),
            // 资产信息
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Text(
                  '清代苏绣花鸟图轴',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: Colors.black,
                  ),
                ),
                const SizedBox(height: 5),
                Text(
                  '锚定金额：¥286,500',
                  style: TextStyle(
                    fontSize: 14,
                    color: ThemeColors.textSecondary,
                  ),
                ),
                const SizedBox(height: 5),
                Text(
                  '锚定日期：2023-05-25',
                  style: TextStyle(
                    fontSize: 14,
                    color: ThemeColors.textSecondary,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  // 构建通证生成信息
  Widget _buildTokenInfo() {
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        color: ThemeColors.cardBg,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Padding(
        padding: const EdgeInsets.all(20),
        child:
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              '通证信息',
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
            ),
            const SizedBox(height: 25),

            // 通证名称
            _buildTokenInfoItem('通证名称', '苏绣花鸟图轴通证'),
            const SizedBox(height: 20),

            // 通证符号
            _buildTokenInfoItem('通证符号', 'SSHT2023'),
            const SizedBox(height: 20),

            // 通证总量
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  '通证总量',
                  style: TextStyle(
                    fontSize: 14,
                    color: ThemeColors.textSecondary,
                  ),
                ),
                const SizedBox(height: 10),
                Container(
                  width: double.infinity,
                  height: 40,
                  decoration: BoxDecoration(
                    color: const Color(0xFFF0F2F5),
                    borderRadius: BorderRadius.circular(6),
                  ),
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 15),
                    child:
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '286,500 枚',
                          style: TextStyle(
                            fontSize: 14,
                            color: Colors.black,
                          ),
                        ),
                        Text(
                          '1 枚 = 1 元',
                          style: TextStyle(
                            fontSize: 12,
                            color: ThemeColors.textSecondary,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
            const SizedBox(height: 20),

            // 通证类型
            _buildTokenInfoItem('通证类型', '非同质化通证 (NFT)'),
          ],
        ),
      ),
    );
  }

  // 构建通证信息项
  Widget _buildTokenInfoItem(String label, String value) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children:
      [
        Text(
          label,
          style: TextStyle(
            fontSize: 14,
            color: ThemeColors.textSecondary,
          ),
        ),
        const SizedBox(height: 10),
        Container(
          width: double.infinity,
          height: 40,
          decoration: BoxDecoration(
            color: const Color(0xFFF0F2F5),
            borderRadius: BorderRadius.circular(6),
          ),
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 15),
            child:
            Row(
              children: [
                Text(
                  value,
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.black,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  // 构建确认按钮
  Widget _buildConfirmButton(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 50,
      decoration: BoxDecoration(
        color: ThemeColors.secondary,
        borderRadius: BorderRadius.circular(25),
        boxShadow: [
          BoxShadow(
            color: ThemeColors.secondary.withOpacity(0.3),
            spreadRadius: 2,
            blurRadius: 5,
            offset: const Offset(0, 3),
          ),
        ],
      ),
      child:
      TextButton(
        onPressed: () {
          // 确认生成通证并跳转到上链完成页面
          Navigator.pushNamed(context, AppRoutes.onchainComplete);
        },
        child:
        const Text(
          '确认生成',
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.bold,
            color: Colors.white,
          ),
        ),
      ),
    );
  }
}